﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TravianHelper.app;
using System.Collections;
using System.IO;
using TravianHelper.hero;

namespace TravianHelper.tools
{
    public partial class FindNatarsForm : Form
    {
        public FindNatarsForm()
        {
            InitializeComponent();
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            TravianApplicationContext.allNatars.Clear();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string filename = openFileDialog1.FileName;
                    StreamReader reader = new StreamReader(filename);
                    string line;
                    while (!reader.EndOfStream)
                    {
                        line = reader.ReadLine();
                        string[] tmp = line.Split(',');
                        Zone zone = new Zone();
                        zone.x = Convert.ToInt32(tmp[0]);

                        zone.y = Convert.ToInt32(tmp[1]);    
                    
                        zone.population = Convert.ToInt32(tmp[2]);    
                        TravianApplicationContext.allNatars.Add(zone);
                    }
                    TravianApplicationContext.allNatars.Sort(new ZoneComparer());
                    updateListViewTarget(TravianApplicationContext.allNatars);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("File format error");
                }
            }
        }

        private void updateListViewTarget(ArrayList zones)
        {
            listViewTarget.Items.Clear();
            foreach (Zone zone in zones)
            {
                listViewTarget.Items.Add(new ListViewItem(new string[] { zone.x.ToString() + "," + zone.y.ToString(), zone.distance.ToString(), zone.status, zone.population.ToString() }));
            }
            totalOasis.Text = "Total natars: " + zones.Count;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string filename = openFileDialog1.FileName;
                    StreamReader reader = new StreamReader(filename);
                    string line;
                    while (!reader.EndOfStream)
                    {
                        line = reader.ReadLine();
                        string[] tmp = line.Split(',');
                        Zone zone = new Zone();
                        zone.x = Convert.ToInt32(tmp[0]);

                        zone.y = Convert.ToInt32(tmp[1]);
                        TravianApplicationContext.allNatars.Add(zone);
                    }
                    TravianApplicationContext.allNatars.Sort(new ZoneComparer());
                    //updateListViewTarget(village.oasis);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("File format error");
                }
            }
        }

        private void buttonSerializeNatars_Click(object sender, EventArgs e)
        {
            Util.SerializeArrayList(TravianApplicationContext.allNatars, "Natars.xml");
        }

        private void buttonSearchNatars_Click(object sender, EventArgs e)
        {
            System.Threading.Thread newThread = new System.Threading.Thread(new UtilTool().getNatarMap);
            newThread.Start();
        }
    }
}
